package henu.javaweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import henu.javaweb.action.IStudentDao;
import henu.javaweb.bean.Student;
import henu.javaweb.dao.DaoFactory;

/**
 * Servlet implementation class CheckStudentInfo
 */
@WebServlet("/UpdateStudentInfo")
public class UpdateStudentInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateStudentInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;Charset=utf-8");
		PrintWriter out=response.getWriter();
		
		//String sno = request.getParameter("sno");
		String sname = request.getParameter("sname");
		String snameused=request.getParameter("ex-name");
		String picpath=request.getParameter("picpath");
		String spellname = request.getParameter("pinyin");
		String address = request.getParameter("address");
		String nation=request.getParameter("nation");
		String politicalStatus=request.getParameter("status");
		String birthday = request.getParameter("birth");
		String remarks = request.getParameter("eg");
		String gender=request.getParameter("sex");
		String phone=request.getParameter("phone");
		String email= request.getParameter("email");
		String major = request.getParameter("major");
		String faculty=request.getParameter("xueyuan");
		String iDnumber=request.getParameter("idnumber");						
				
		IStudentDao studentDao=DaoFactory.getStudentDaoInstance();
	    String s=(String)request.getSession().getAttribute("sno");
	    Student stu=studentDao.findBySno(s);
	   // stu.setS_no(sno);
	    stu.setS_name(sname);
	    stu.setS_photo(picpath);
	    stu.setS_phone(phone);
	    stu.setS_nation(nation);
	    stu.setS_major(major);
	    stu.setS_nameUsed(snameused);
	    stu.setS_email(email);
	    stu.setS_IDCard(iDnumber);
	    stu.setS_gender(gender);
	    stu.setS_faculty(faculty);
	    stu.setS_birthdate(birthday);
	    stu.setS_nativePlace(address);
	    stu.setS_politicalStatus(politicalStatus);
	    stu.setS_remarks(remarks);
	    stu.setS_spellname(spellname);
	    
	   
	    int result= 0;
	    result= studentDao.update(stu);
	    if(result>0) {
	    	//request.getSession().setAttribute("student",stu);

	    	out.print("<script type=\"text/javascript\">alert('修改个人信息成功!');history.go(-1);</script>");
	    }else {
	    	out.print("<script type=\"text/javascript\">alert('修改个人信息失败，未保存到数据库，请再次保存!');history.go(-1);</script>");
	    }
	}
}
